package game.scene.sceneobject;

import common.Coordinate;
import common.Rectangle;

import engine.video.blit.Blitter;
import engine.video.blit.SpriteInfo;
import game.spritesheet.RegularSpriteSheet;
import game.spritesheet.SpriteSheet_I;

public class Text_SO extends SceneObject_A {
	public Text_SO(String text, SpriteSheet_I font) {
		Font = font;
		setText(text);
	}
	public SpriteSheet_I Font;
	public String Text;
	private boolean _ReInit;
	
	public void setText(String text) {
		Text = text;
		this.VisRect.Width = text.length() * Font.getSprite(0).ClipRect.Width;
		this.VisRect.Height = Font.getSprite(0).ClipRect.Height;
		_ReInit = true;
	}
	@Override
	public void setLocation(Coordinate location) {
		this.VisRect.X = location.X;
		this.VisRect.Y = location.Y;
	}
	@Override
	public void setBoundry(Rectangle bounds) {
		return;
	}
	
	public void setCenterOn(Coordinate location) {
		int x = location.X;
		int y = location.Y;
		this.VisRect.X = x - (this.VisRect.Width / 2);
		this.VisRect.Y = y - (this.VisRect.Height / 2);
	}
	
	@Override
	public boolean isOverlapping(Rectangle queryRect) {
		if(_ReInit == true) {
			return true;
		} else {
			return this.VisRect.overlaps(queryRect);
		}
	}

	@Override
	public void render(Coordinate camera, Blitter blitter) {
		if(Font == null) {
			return;
		}
		SpriteInfo sprite = null;
		int xOfs = this.VisRect.X - camera.X;
		int yOfs = this.VisRect.Y - camera.Y;
		int textWidth = 0;
		for(char c : Text.toCharArray()) {
			int index = (int)c;
			sprite = Font.getSprite(index);
			blitter.blit(sprite, xOfs + textWidth, yOfs);
			textWidth += sprite.ClipRect.Width;
		}
		if(_ReInit == true) {
			this.VisRect.Width = textWidth;
			this.VisRect.Height = sprite.ClipRect.Height;
		}
	}
	
}
